package com.sweetsourfish.crm.web;

import com.sweetsourfish.crm.commons.model.Constants;
import com.sweetsourfish.crm.settings.model.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//       登录拦截：判断session有没有user对象
        User loginUser = (User) request.getSession().getAttribute(Constants.SESSION_USER);
        if (loginUser==null){
//            拦截
//            跳转页面
            response.sendRedirect(request.getContextPath()+"/settings/user/login");
            return false;
        }else {
            return true;
        }
    }
}
